﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Project_KhuyenMaiVang.Models;
using Project_KhuyenMaiVang.DataProvider;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

namespace Project_KhuyenMaiVang.DAO
{
    public class KhuVucDAO:MyDataProvider
    {
        // Thêm khu vực mới
        // parameter    : KhuVucDTO
        // return true nếu thành công, false nếu thất bại
        public bool insert(KhuVucDTO area)
        {
            bool result = true;
            try
            {
                string strSql = "INSERT INTO KhuVuc (TenKhuVuc) VALUES (@TenKhuVuc)";
                _Command = new SqlCommand(strSql, _Connection);
                SqlParameter[] param = new SqlParameter[1];

                param[0] = new SqlParameter("@TenKhuVuc", SqlDbType.NVarChar, 100);

                param[0].Value = area.TenKhuVuc;

                for (int i = 0; i < param.Length; i++)
                {
                    _Command.Parameters.Add(param[i]);
                }

                _Command.CommandType = CommandType.Text;
                _Command.ExecuteNonQuery();
            }
            catch
            {
                result = false;
            }

            return result;
        }

        // Lấy tên khu vực theo mã khu vực
        // parameter    : idKhuVuc
        // return       : string khu vực
        public string getNameArea(int idKhuVuc)
        {
            KhuVucDTO result = new KhuVucDTO();
            try
            {
                string strCommand = "SELECT* FROM KhuVuc WHERE MaKhuVuc = " + idKhuVuc.ToString();
                _Adapter = new SqlDataAdapter(strCommand, _Connection);
                DataSet dataset = new DataSet();
                _Adapter.Fill(dataset);
                DataTable dt = dataset.Tables[0];
                object ob = GetDataFromDataRow(dt, 0);
                result = (KhuVucDTO)ob;
            }
            catch (Exception ex)
            {
                result = null;
            }

            return result.TenKhuVuc;
        }

        public List<KhuVucDTO> GetListAreas()
        {
            List<KhuVucDTO> result=new List<KhuVucDTO>();
            try
            {
                string strCommand = "SELECT * FROM KhuVuc";
                _Adapter = new SqlDataAdapter(strCommand, _Connection);
                DataSet dataset = new DataSet();
                _Adapter.Fill(dataset);
                DataTable dt = dataset.Tables[0];

                int n = dt.Rows.Count;
                for(int i=0;i<n;i++)
                {
                    object ob = GetDataFromDataRow(dt, i);    
                    result.Add((KhuVucDTO)ob);
                }
                return result;
            }
            catch (Exception ex)
            {
                result = null;
            }
            return null;
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            KhuVucDTO order = new KhuVucDTO();
            order.MaKhuVuc = Int32.Parse(dt.Rows[i]["MaKhuVuc"].ToString());
            order.TenKhuVuc = dt.Rows[i]["TenKhuVuc"].ToString();
            return (object)order;
        }
    }
}